﻿using CSC.Managers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace JDGLSys.Forms.Pages.AdminFormPages
{
    /// <summary>
    /// UsersPage.xaml 的交互逻辑
    /// </summary>
    public partial class UsersPage : Page
    {
        public UsersPage()
        {
            InitializeComponent();
        }

        private void Updatas_Click(object sender, RoutedEventArgs e)
        {
            DataGrid.ItemsSource =
            UsersTManager.ShowDataTable().DefaultView;

            //更改datagrid标题      UsersT表共8列
            DataGrid.Columns[0].Header = "用户id";
            DataGrid.Columns[1].Header = "邮箱";
            DataGrid.Columns[2].Header = "账号";

            DataGrid.Columns[3].Header = "密码";
            DataGrid.Columns[4].Header = "消费金额";
            DataGrid.Columns[5].Header = "会员等级";

            DataGrid.Columns[6].Header = "卡号";
            DataGrid.Columns[7].Header = "管理员id";
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            //默认显示表格内容
            DataGrid.ItemsSource =
            UsersTManager.ShowDataTable().DefaultView;
            //更改datagrid标题      UsersT表共8列
            DataGrid.Columns[0].Header = "用户id";
            DataGrid.Columns[1].Header = "邮箱";
            DataGrid.Columns[2].Header = "账号";

            DataGrid.Columns[3].Header = "密码";
            DataGrid.Columns[4].Header = "消费金额";
            DataGrid.Columns[5].Header = "会员等级";

            DataGrid.Columns[6].Header = "卡号";
            DataGrid.Columns[7].Header = "管理员id";
        }

        private void UpdateBtn_Click(object sender, RoutedEventArgs e)
        {
            UsersTManager.UpdateSqlDBBuilder();
            MessageBox.Show("数据已更新到库");
        }

        private void deleteBtn_Click(object sender, RoutedEventArgs e)
        {
            //删除所选择相关记录
            MessageBoxResult r = MessageBox.Show("你确定要删除吗？", "信息提示", MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (r == MessageBoxResult.Yes)
            {
                DataRowView drv = DataGrid.SelectedItem as DataRowView;
                drv.Delete();

                UsersTManager.UpdateSqlDBBuilder();
                MessageBox.Show("所选记录已全部删除");
            }
            else return;
        }


        private void WriteDcCar_Btn_Click(object sender, RoutedEventArgs e)
        {
            //获取当前选中行
            DataRowView drv = DataGrid.SelectedItem as DataRowView;
            //以当前时间生成卡号
            string autoCarNum = DateTime.Now.ToString("yyyyMMddHHmmss");
            Car_Write(selectSanqu(2), dizhi2, autoCarNum);
            drv[6] = autoCarNum;
        }
        int dizhi1, dizhi2, dizhi3;
        private int selectSanqu(int sanqu)
        {
            dizhi1 = sanqu * 4;
            dizhi2 = dizhi1 + 1;
            dizhi3 = dizhi2 + 1;
            return sanqu;
        }

        private void Car_Write(int sanqu, int dizhi,string carNum)
        {
            //设备初始化
            //icdev：通讯设备标识符
            int icdev = DcHelper.dc_init(100, 115000);
            if (icdev > 0)
            {
                // 1.寻卡
                long kaxuliehao = 0;
                //寻卡模式--0，单卡模式
                int xunkaMode = 0;

                //寻卡返回结果为0表示成功
                int xunkaR = DcHelper.dc_card(icdev, xunkaMode, ref kaxuliehao);
                if (xunkaR == 0)
                {
                    //2.装载密码
                    //读取到的密码
                    byte[] carPassword = new byte[] { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
                    //返回0表示成功
                    int zhuangzaiR = DcHelper.dc_load_key(icdev, xunkaMode, sanqu, carPassword);
                    if (zhuangzaiR == 0)
                    {
                        //3.验证密码
                        int yanzhengR = DcHelper.dc_authentication(icdev, xunkaMode, sanqu);
                        if (yanzhengR == 0)
                        {
                            //4.写卡
                            int writeR = DcHelper.dc_write(icdev, dizhi, carNum);
                            if (writeR == 0)
                            {
                                DcHelper.dc_beep(icdev, 10);
                                MessageBox.Show("写卡成功");
                            }
                            else
                            {
                                MessageBox.Show("卡出现异常，请检查卡是否损坏");
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show("寻卡失败");
                    }
                }
                else
                {
                    MessageBox.Show("卡出现异常，请检查卡是否损坏");
                }
            }
            //蜂鸣
            //DcHelper.dc_beep(icdev, 100);
            //测试
            //MessageBox.Show(icdev.ToString());

        }
    }
}
